class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        if (nums.size() == 1 || is_sorted(nums.begin(), nums.end()))
            return 0;
        vector<int> temp(nums);
        sort(nums.begin(), nums.end());
        int ret = 0;
        int i = 0;
        int left = 0;
        int right = nums.size() - 1;
        while (temp[left] == nums[left])
        {
            left++;
        }
        while (temp[right] == nums[right])
        {
            right--;
        }

        return right - left + 1;
    }
};